{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 异常"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "首先，`try` 语句下的（`try` 和 `except` 之间的代码）被执行，如果没有出现异常，`except` 语句将被忽略。\n",
    "如果 `try` 语句之间出现了异常，`try` 之下异常之后的代码被忽略，直接跳跃到 `except` 语句。\n",
    "如果异常出现，但并不属于 `except` 中定义的异常类型，程序将执行外围一层的 `try` 语句，如果异常没有被处理，将产生 `unhandled exception` 的错误。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Not valid input, try again ...\n",
      "123\n"
     ]
    }
   ],
   "source": [
    "for item in list(['98k', 123, 234]):\n",
    "    try:\n",
    "        x = int(item)\n",
    "        print(x) # 不会再尝试 234 了，因为 123 就是正常数字\n",
    "        break\n",
    "    except ValueError:\n",
    "        print(\"Not valid input, try again ...\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
